package com.shengyin.ec.controller.delievery;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.Delivery;
import com.shengyin.ec.service.delivery.DelieveryService;

@Controller
public class DelieveryController {
	
	@Autowired
	DelieveryService delieveryService;

	@RequestMapping("/toDelieveryAdd.do")
	public String toDelieveryAdd() {

		return "redirect:/admin/delievery/delieveryAdd.jsp";
	}

	@RequestMapping("/toDelieveryModify.do")
	public String toDelieveryModify(Model model,@RequestParam("id") long id) {
		Delivery delievery=delieveryService.getDeliveryById(id);
		
		model.addAttribute("delievery", delievery);
		return "/admin/delievery/delieveryModify";
	}

	@RequestMapping("/toDelieveryList.do")
	public String toDelieveryList(Model model) {
		List<Delivery> deliveryList = delieveryService.getDelieveryList();
		model.addAttribute("deliveryList", deliveryList);
		return "/admin/delievery/delieveryList";
	}
	
	@RequestMapping("/delieveryAdd.do")
	public String createDelievery(DelieveryForm delieveryForm){
		
		try {
			Delivery delievery=new Delivery();
			BeanUtils.copyProperties(delievery, delieveryForm);
			delieveryService.createDelievery(delievery);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:/toDelieveryList.do";
	}
	
	@RequestMapping("/delieveryDelete.do")
    public String deleteDelievery(@RequestParam("id") int id){
		delieveryService.removeDelieveryById(id);
		return "redirect:/toDelieveryList.do";
	}
    
    @RequestMapping("/delieveryModify.do")
    public String modifyDelievery(DelieveryForm delieveryForm) {
    	
    	try {
    		Delivery delivery=new Delivery();
			BeanUtils.copyProperties(delivery, delieveryForm);
			delieveryService.modifyDelievery(delivery);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return "redirect:/toDelieveryList.do";
    }
    
    
    /*
     * spring mvc异常处理方法，在controller发生异常，可以以抛出ControllerException，以供捕获
     * */
    @ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  

}
